package com.btpn.custody.bean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.btpn.custody.common.Constant;
import com.btpn.infra.utils.MyRegexValidator;

@Component("branchEmailSender")
public class BranchEmailSender extends EmailSender {
	
	private final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private EmailSender emailSender;
	
	private StringBuffer sb;	
	private final String subject 	= "Butuh Kelengkapan";
	private final String footer		= "Terima Kasih";
	
	public String sendMailToBranchUser(String cif, String tipeNasabah, String fromMail, String toMail, String uncompleteMsg) {
				
		// declare text message
		sb = new StringBuffer();
		sb.append("Dear, " + toMail
				+ System.getProperty("line.separator")
				+ System.getProperty("line.separator")
				+ "Kelengkapan pembukaan account dari nasabah dengan :"
				+ "\nCIF : " + cif
				+ "\nTipe Nasabah : " + tipeNasabah
				+ "\nKekurangan : \n"
				+ uncompleteMsg
				+ System.getProperty("line.separator")
				+ "Mohon untuk segera dilengkapi"
				+ System.getProperty("line.separator")
				+ footer);
		try {
			if (MyRegexValidator.validateEmail(fromMail) & (MyRegexValidator.validateEmail(toMail))) {
				
				emailSender.sendMail(fromMail, toMail, subject, sb.toString());
								
				return Constant.STATUS__SUCCESS;
			} else {
				logger.error("Exception : Email not valid: From({}) or To({})", fromMail, toMail);
				
				return Constant.STATUS__ERROR;
			}
		} catch(Exception e) {
			logger.error("Exception : ", e);
			return Constant.STATUS__ERROR;
		} finally {
			sb=null;
		}
	}
}
